package com.example.administrator.netdemo.network;

import com.example.administrator.netdemo.network.base.BaseModel;
import com.jiongbull.jlog.JLog;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by luquan on 2017/6/27.
 */

public class RequestModelManager {
    private RequestModelManager() {}
    private static class SingletonHolder{
        private static final RequestModelManager INSTANCE = new RequestModelManager();
    }
    public static RequestModelManager getInstance(){
        return SingletonHolder.INSTANCE;
    }


    public Map<String,Set<BaseModel>> bModelsMap = new HashMap();

    public void markReqModel(String key,BaseModel model){
        Set<BaseModel> bModelsSet = bModelsMap.get(key);
        if(bModelsSet==null){
            bModelsSet = new HashSet<>();
            bModelsMap.put(key,bModelsSet);
        }
        bModelsSet.add(model);
    }

    public void cancelAllRequest(String key){
        JLog.d("lq_Request","cancelAllRequest  = "+key);
        Set<BaseModel> bModelsSet = bModelsMap.get(key);
        if(bModelsSet!=null){
            Iterator<BaseModel> it = bModelsSet.iterator();
            while (it.hasNext()) {
                BaseModel bModel = it.next();
                bModel.cancelAllRequest();
            }
            bModelsSet.clear();
        }
    }

    public void cancelAllRequestExceptThisTag(String key,String specialTag){
        Set<BaseModel> bModelsSet = bModelsMap.get(key);
        if(bModelsSet!=null){
            Iterator<BaseModel> it = bModelsSet.iterator();
            while (it.hasNext()) {
                BaseModel bModel = it.next();
                bModel.cancelAllRequestExceptThisTag(specialTag);
            }
            bModelsSet.clear();
        }

    }

    public void cancelAllRequestExceptThoseTags(String key,String... specialTags){
        Set<BaseModel> bModelsSet = bModelsMap.get(key);
        if(bModelsSet!=null){
            Iterator<BaseModel> it = bModelsSet.iterator();
            while (it.hasNext()) {
                BaseModel bModel = it.next();
                bModel.cancelAllRequestExceptThoseTags((Object) specialTags);
            }
            bModelsSet.clear();
        }

    }

}
